package com.ipay.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.ipay.entity.PayChannelEntity;
import com.ipay.service.PayChannelService;
import com.ipay.utils.Constant;
import com.ipay.utils.PageUtils;
import com.ipay.utils.Query;
import com.ipay.utils.R;

/**
 * 支付渠道商管理
 * 
 * @author Luohx
 */
@Transactional
@Controller
@RequestMapping("/payChannel")
public class PayChannelController extends AbstractController {
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private PayChannelService payChannelService;

	@RequestMapping("/{url}.html")
	public String page(@PathVariable("url") String url) {
		return "paychannel/" + url + ".html";
	}
	
	
	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@ResponseBody
	@RequiresPermissions("payChannel:list")
	public R list(@RequestParam Map<String, Object> params) {
		logger.info("params==" + params.toString());
		// 查询列表数据
		Query query = new Query(params);
		logger.info("query==" + query.toString());

		List<PayChannelEntity> payChannelList = payChannelService.queryList(query);
		int total = payChannelService.queryTotal(query);
		logger.info("total==" + total);

		PageUtils pageUtil = new PageUtils(payChannelList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}
	
	/**
	 * 信息
	 * @throws IOException 
	 */
	@RequestMapping("/info/{id}")
	@ResponseBody
	@RequiresPermissions("payChannel:info")
	public R info(@PathVariable("id") String id,HttpServletRequest request, HttpServletResponse response) throws IOException {
		PayChannelEntity payChannel = payChannelService.queryObject(id);
		
	    
		return R.ok().put("payChannel", payChannel);
	}
	
	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@ResponseBody
	@RequiresPermissions("payChannel:save")
	public R save(@RequestBody PayChannelEntity payChannel) {
		//根据商户号 渠道类型 状态为开启状态 查询 支付渠道商表
		/*Map<String,Object> map = new HashMap<String,Object>();
		map.put("merchantNo", payChannel.getMerchantNo());
		if(payChannel.getPayType() == null){
			return R.error("请选择一个支付渠道类型");
		}
		map.put("payType", payChannel.getPayType());
		map.put("bankId", payChannel.getBankId());
		map.put("enable", String.valueOf(Constant.Status.ENABLE.getValue()));
		List<PayChannelEntity> list = payChannelService.queryList(map);
		if(list.size() > 0){
			return R.error("该银行信息已关联该支付渠道类型");
		}*/
		//根据商户号、bankId、渠道类型 、渠道状态
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("merchantNo", payChannel.getMerchantNo());
		map.put("bankId", payChannel.getBankId());
		map.put("payType", payChannel.getPayType());
		PayChannelEntity pay = payChannelService.queryInfoByParam(map);
		String msg = "";
		if(pay != null){
			return R.error("该银行卡已关联该渠道类型");
		}else{
			payChannelService.save(payChannel);
			msg = "新增成功";
		}
		return R.ok(msg);
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@ResponseBody
	@RequiresPermissions("payChannel:update")
	public R update(@RequestBody PayChannelEntity payChannel) {
		if(payChannelService.update(payChannel) > 0){
			return R.ok("修改成功");
		}
		return R.error("修改失败");
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	@ResponseBody
	@RequiresPermissions("payChannel:delete")
	public R delete(@RequestBody String[] ids) {
		payChannelService.deleteBatch(ids);
		return R.ok();
	}

}
